#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2019
# @Author  : linhu (hlf0312@gmail.com)
# @Link    : TBD
# @Version : $Id$

from datetime import datetime
from dateutil.relativedelta import relativedelta

class FeatureBuild(object):
	"""docstring for FeatureBuild"""
	def __init__(self, fbId, startDate, endDate):
		self.fbId = fbId
		self.startDate = startDate
		self.endDate = endDate

	def getFB(self):
		return self.fbId

	def setFB(self, fbId):
		self.fbId = fbId

	def setStartDate(self, date):
		self.startDate = date

	def setEndDate(self, date):
		self.endDate = date

	def getStartDate(self):
		return self.startDate

	def getEndDate(self):
		return self.endDate

def buildFeatureBuildsTimeline():
	featureBuildsTimeLine = []
	# FB2201 = FeatureBuild('2201', '2022-01-05', '2022-01-18')
	FB2201 = FeatureBuild('2201', '2022-01-04', '2022-01-17')
	# print(startFB.getFB(), startFB.getStartDate(), startFB.getEndDate())
	startFB = FB2201
	fbNumInOneYear = 26
	fbDurationDays = 14
	nextNYear = 4
	for i in range(1, nextNYear*fbNumInOneYear):
		nextFBStartDate = (datetime.strptime(startFB.getStartDate(), '%Y-%m-%d') + relativedelta(days=+fbDurationDays)).strftime('%Y-%m-%d')
		nextFBEndDate = (datetime.strptime(startFB.getEndDate(), '%Y-%m-%d') + relativedelta(days=+fbDurationDays)).strftime('%Y-%m-%d')
		if int(startFB.getFB())%100 == fbNumInOneYear:
			nextFBId = str(100 * (int(startFB.getFB())//100 + 1) + 1)	
		else:
			nextFBId = str(int(startFB.getFB()) + 1)
		
		nextFB = FeatureBuild(nextFBId, nextFBStartDate, nextFBEndDate)
		featureBuildsTimeLine.append(nextFB)
		startFB = nextFB
		# print(nextFB.getFB(), nextFBStartDate, nextFBEndDate)

	return featureBuildsTimeLine

def getFeatureBuild(date, featureBuildsSchedule):
	for fb in featureBuildsSchedule:
		if date >= fb.getStartDate() and date <= fb.getEndDate():
			return fb

def getFeatureBuildByFbId(fbId, featureBuildsSchedule):
	for fb in featureBuildsSchedule:
		if fb.getFB() == fbId:
			return fb

	print('No FB: {} found'.format(fbId))


if __name__ == '__main__':
	fbSchedule = buildFeatureBuildsTimeline()
